<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: FriendsController</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">FriendsController</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../files/app/controllers/friends_controller_rb.html">
                app/controllers/friends_controller.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                <a href="ApplicationController.html">
                ApplicationController
               </a>
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">



   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000046">add</a>&nbsp;&nbsp;
      <a href="#M000045">browse</a>&nbsp;&nbsp;
      <a href="#M000042">index</a>&nbsp;&nbsp;
      <a href="#M000044">list</a>&nbsp;&nbsp;
      <a href="#M000047">remove</a>&nbsp;&nbsp;
      <a href="#M000043">search</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000046" class="method-detail">
        <a name="M000046"></a>

        <div class="method-heading">
          <a href="#M000046" class="method-signature">
          <span class="method-name">add</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
<a href="FriendsController.html#M000046">add</a> a friend to the logged in
user by id
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000046-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000046-source">
<pre>
    <span class="ruby-comment cmt"># File app/controllers/friends_controller.rb, line 35</span>
35:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">add</span>
36:     <span class="ruby-identifier">user</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">find</span> <span class="ruby-identifier">session</span>[<span class="ruby-identifier">:user</span>]
37:     <span class="ruby-identifier">friend</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">find</span> <span class="ruby-identifier">params</span>[<span class="ruby-identifier">:id</span>]
38:     
39:     <span class="ruby-identifier">redirect_to</span> <span class="ruby-identifier">:back</span>
40:     
41:     <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">user</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">friend</span>
42:       <span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-identifier">user</span>.<span class="ruby-identifier">friends</span>.<span class="ruby-identifier">exists?</span>(<span class="ruby-identifier">params</span>[<span class="ruby-identifier">:id</span>])
43:         <span class="ruby-identifier">user</span>.<span class="ruby-identifier">friends</span>.<span class="ruby-identifier">concat</span> <span class="ruby-identifier">friend</span>
44:         <span class="ruby-identifier">friend</span>.<span class="ruby-identifier">friends</span>.<span class="ruby-identifier">concat</span> <span class="ruby-identifier">user</span>
45:       <span class="ruby-keyword kw">else</span>
46:         <span class="ruby-identifier">flash</span>[<span class="ruby-identifier">:error</span>] = <span class="ruby-value str">'Sorry, you cannot add the same friend more than once.'</span>
47:       <span class="ruby-keyword kw">end</span>
48:     <span class="ruby-keyword kw">else</span>
49:       <span class="ruby-identifier">flash</span>[<span class="ruby-identifier">:error</span>] = <span class="ruby-value str">'Sorry, you cannot be your own friend.'</span>
50:     <span class="ruby-keyword kw">end</span>
51:     
52:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000045" class="method-detail">
        <a name="M000045"></a>

        <div class="method-heading">
          <a href="#M000045" class="method-signature">
          <span class="method-name">browse</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
<a href="FriendsController.html#M000044">list</a> all users
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000045-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000045-source">
<pre>
    <span class="ruby-comment cmt"># File app/controllers/friends_controller.rb, line 25</span>
25:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">browse</span>
26:     <span class="ruby-ivar">@title</span> = <span class="ruby-value str">&quot;Browse Users&quot;</span>
27:     <span class="ruby-ivar">@paginate</span> = <span class="ruby-keyword kw">true</span>
28:     <span class="ruby-ivar">@user</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">find</span> <span class="ruby-identifier">session</span>[<span class="ruby-identifier">:user</span>]
29:     <span class="ruby-ivar">@user_count</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">count_by_sql</span> <span class="ruby-value str">&quot;SELECT COUNT(*) FROM users&quot;</span>
30:     <span class="ruby-ivar">@friends</span>, <span class="ruby-ivar">@friends_page</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">paginate</span> <span class="ruby-identifier">:all</span>, <span class="ruby-identifier">:order</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">'last_name ASC, first_name ASC'</span>, <span class="ruby-identifier">:page</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">params</span>[<span class="ruby-identifier">:page</span>]
31:     <span class="ruby-identifier">render</span> <span class="ruby-identifier">:action</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">'list'</span>
32:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000042" class="method-detail">
        <a name="M000042"></a>

        <div class="method-heading">
          <a href="#M000042" class="method-signature">
          <span class="method-name">index</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
<a href="FriendsController.html#M000044">list</a> logged in user&#8216;s
friends
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000042-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000042-source">
<pre>
   <span class="ruby-comment cmt"># File app/controllers/friends_controller.rb, line 3</span>
3:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">index</span>
4:     <span class="ruby-identifier">redirect_to</span>(<span class="ruby-identifier">:action</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">&quot;list&quot;</span>)
5:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000044" class="method-detail">
        <a name="M000044"></a>

        <div class="method-heading">
          <a href="#M000044" class="method-signature">
          <span class="method-name">list</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
<a href="FriendsController.html#M000044">list</a> logged in user&#8216;s
friends
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000044-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000044-source">
<pre>
    <span class="ruby-comment cmt"># File app/controllers/friends_controller.rb, line 19</span>
19:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">list</span>
20:     <span class="ruby-ivar">@user</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">find</span> <span class="ruby-identifier">session</span>[<span class="ruby-identifier">:user</span>]
21:     <span class="ruby-ivar">@friends</span> = <span class="ruby-ivar">@user</span>.<span class="ruby-identifier">friends</span>
22:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000047" class="method-detail">
        <a name="M000047"></a>

        <div class="method-heading">
          <a href="#M000047" class="method-signature">
          <span class="method-name">remove</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
<a href="FriendsController.html#M000047">remove</a> a friend from the
logged in user by id
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000047-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000047-source">
<pre>
    <span class="ruby-comment cmt"># File app/controllers/friends_controller.rb, line 55</span>
55:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">remove</span>
56:     <span class="ruby-identifier">user_id</span> = <span class="ruby-identifier">session</span>[<span class="ruby-identifier">:user</span>]
57:     <span class="ruby-identifier">friend_id</span> = <span class="ruby-identifier">params</span>[<span class="ruby-identifier">:id</span>]
58: 
59:     <span class="ruby-identifier">redirect_to</span> <span class="ruby-identifier">:back</span>
60: 
61:     <span class="ruby-comment cmt">#user = User.find session[:user]</span>
62:     <span class="ruby-comment cmt">#friend = User.find params[:id]</span>
63: 
64:     <span class="ruby-constant">Relationship</span>.<span class="ruby-identifier">delete_all</span> [<span class="ruby-value str">&quot;(user_id = ? AND friend_id = ?) OR (user_id = ? AND friend_id = ?)&quot;</span>, <span class="ruby-identifier">user_id</span>, <span class="ruby-identifier">friend_id</span>, <span class="ruby-identifier">friend_id</span>, <span class="ruby-identifier">user_id</span>]
65:     
66:     <span class="ruby-comment cmt">#WARN</span>
67:     <span class="ruby-comment cmt">#broken in rails</span>
68:     <span class="ruby-comment cmt">#user.friends.delete friend</span>
69:     <span class="ruby-comment cmt">#friend.friends.delete user</span>
70:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000043" class="method-detail">
        <a name="M000043"></a>

        <div class="method-heading">
          <a href="#M000043" class="method-signature">
          <span class="method-name">search</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
<a href="FriendsController.html#M000043">search</a> for user by string
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000043-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000043-source">
<pre>
    <span class="ruby-comment cmt"># File app/controllers/friends_controller.rb, line 8</span>
 8:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">search</span>
 9:     <span class="ruby-ivar">@user</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">find</span> <span class="ruby-identifier">session</span>[<span class="ruby-identifier">:user</span>]
10:     <span class="ruby-keyword kw">if</span> (<span class="ruby-identifier">params</span>[<span class="ruby-identifier">:q</span>])
11:       <span class="ruby-ivar">@friends</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">search</span>(<span class="ruby-identifier">params</span>[<span class="ruby-identifier">:q</span>])
12:     <span class="ruby-keyword kw">else</span>
13:       <span class="ruby-ivar">@friends</span> = []
14:     <span class="ruby-keyword kw">end</span>
15:     <span class="ruby-identifier">render</span> <span class="ruby-identifier">:action</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value str">'list'</span>
16:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>